﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FluentScheduler;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Business;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.EFDBFirst;
using YiYiTong.Rocket.Business.Biz;

namespace YiYiTong.Rocket.ProcessConsole
{
    /// <summary>
    /// 活动
    /// </summary>
    public class HuodongJob : BaseJob
    {
        static int excutecounts;
        public override void DoAction()
        {
            if (excutecounts % 100 == 0)
            {
                LogHelper.Debug($"活动执行：{excutecounts}次");
            }
            excutecounts++;

            ADZ_MasterEntities dbcontext = new ADZ_MasterEntities();
            //检查未开始的活动为开始

            var huodong_list = dbcontext.ADZ_HuodongMaster.Where(t => t.begin_time <= DateTime.Now && t.huodong_status == (int)HuodongStatus.Default).ToList();
            if(huodong_list.Any())
            {
                huodong_list.ForEach(t =>
                {
                    t.huodong_status = (int)HuodongStatus.InProgress;

                    //发送业务消息
                    BusinessMsgBiz.Instance.SendBusinessMsg(new ADZ_BusinessMsg
                    {
                        BusinessMsgType = (int)BusinessMsgEnum.HuodongBegin,
                        msg = $"活动 {t.huodong_title} 已经开始！",
                        msg_title = "活动开始",
                        msg_url = $""
                    });
                });
                dbcontext.SaveChanges();
                ReleaseBiz.Instance.SetNewReleaseNo(ReleaseListType.Huodong);


            }

            var finish_list = dbcontext.ADZ_HuodongMaster.Where(t => t.end_time <= DateTime.Now && t.huodong_status < (int)HuodongStatus.Finished).ToList();
            if(finish_list.Any())
            {
                //结束活动
                finish_list.ForEach(t =>
                {
                    t.huodong_status = (int)HuodongStatus.Finished;

                    //发送业务消息
                    BusinessMsgBiz.Instance.SendBusinessMsg(new ADZ_BusinessMsg
                    {
                        BusinessMsgType = (int)BusinessMsgEnum.HuodongEnd,
                        msg = $"活动 {t.huodong_title} 已经结束！",
                        msg_title = "活动结束",
                        msg_url = $""
                    });
                });
                //活动商品是否下架 todo
                dbcontext.SaveChanges();
            }
        }
    }
}
